package cn.kgc.vue.controller;

import cn.kgc.vue.annotations.Logs;
import cn.kgc.vue.commons.Const;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.User;
import cn.kgc.vue.enums.Operation;
import cn.kgc.vue.form.LoginForm;
import cn.kgc.vue.form.UserForm;
import cn.kgc.vue.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author 课工场
 * @date 2024/4/9
 * @description
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户模块")
public class UserController {

    @Resource
    private UserService userService;

    // 登录   {username,password,code,uuid}  认证通过   -> token
    @PostMapping("login")
    @ApiOperation("登录接口")
    // @Logs(description = Operation.SELECT,model = "用户模块")
    public ResponseResult login(@RequestBody LoginForm loginForm, HttpServletRequest request) {

        return userService.login(loginForm, request);
    }

    @PostMapping("page")
    @ApiOperation("用户分页数据")
    @Logs(description = Operation.SELECT,model = "用户模块")
    public ResponseResult getList(@RequestBody QueryCondition<User> queryCondition) {

        return userService.userPage(queryCondition);
    }


    @DeleteMapping("delete")
    @ApiOperation("删除用户")
    @RequiresPermissions("sys:user:delete")
    @Logs(description = Operation.DELETE,model = "用户模块")
    public ResponseResult delete(@RequestBody List<Integer> uids) {

        return userService.delete(uids);
    }

    @PostMapping("add")
    @ApiOperation("添加用户")
    @RequiresPermissions("sys:user:add")
    @Logs(description = Operation.ADD,model = "用户模块")
    public ResponseResult add(@RequestBody UserForm userForm) {

        return userService.addUser(userForm);
    }

    @PutMapping("edit")
    @ApiOperation("编辑用户")
    @RequiresPermissions("sys:user:update")
    @Logs(description = Operation.UPDATE,model = "用户模块")
    public ResponseResult edit(@RequestBody UserForm userForm) {

        return userService.editUser(userForm);
    }

    @GetMapping("getById/{id}")
    @ApiOperation("获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户id")
    public ResponseResult getById(@PathVariable("id") Integer id) {
        return userService.getByUId(id);
    }

    @PutMapping("state/{id}/{state}")
    @ApiOperation("修改用户状态")
    @RequiresPermissions("sys:user:state")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id"),
            @ApiImplicitParam(name = "state", value = "用户状态")
    })

    public ResponseResult editState(@PathVariable("id") Integer id, @PathVariable Integer state) {


        return userService.editState(id, state);
    }

    @PutMapping("resetPassword/{id}")
    @ApiOperation("重置密码")
    @ApiImplicitParam(name = "id", value = "用户id")
    public ResponseResult resetPassword(@PathVariable("id") Integer id) {

        return userService.resetPassword(id);
    }


    // 获取个人信息
    @GetMapping("personal")
    @ApiOperation("获取个人信息")
    public ResponseResult getPersonalInfo(HttpServletRequest request) {
        return  userService.getPersonalInfo(request);
    }


    // 编辑个人信息
    @PutMapping("editPersonal")
    @ApiOperation("编辑个人信息")
    public ResponseResult editPersonalInfo(@RequestBody  User user) {

        userService.updateById(user);

        return ResponseResult.success().message("个人信息修改成功");
    }


    // 修改个人密码
    @PutMapping("editPersonalPwd")
    @ApiOperation("修改个人密码")
    public ResponseResult editPersonalPwd(@RequestBody Map<String,String> pwdInfo,HttpServletRequest request) {

        System.out.println("pwdInfo = " + pwdInfo);

        return userService.editPersonalPwd(pwdInfo,request);
    }


    // 退出登录
    @GetMapping("logout")
    @ApiOperation("退出登录")
    public ResponseResult logout(HttpServletRequest request) {


        return userService.logout(request);
    }


    // 获取用户菜单
    @GetMapping("menu")
    @ApiOperation("菜单接口")
    public ResponseResult userMenu(HttpServletRequest request) {


        return userService.userMenu(request);
    }

    // 用户未登录时的请求
    @RequestMapping("unAuthenticate")
    public ResponseResult unAuthenticate() {
        return ResponseResult.fail().code(Const.UN_AUTHENTICATE).message("请认证通过后访问");
    }
}
